/**
 * 支持卡号类型
 * name 卡号名称
 * pattern 用于验证完整卡号的正则表达式
 * eagerPattern 从部分数字猜测卡片类型的正则表达式
 * imgUrl oss 图片地址
 **/
const cardTypes = [
    {
        name: "American Express", //美国运通卡
        pattern: /^3[47]\d{13}$/, //校验规则开头为第一位3 第二位 4或者7 拼接13位数字
        eagerPattern: /^3[47]/, // 校验规则开头为第一位3 第二位 4或者7
        imgUrl: "/statics/cart/accepted_payment_brand/american_express.svg"
    },
    {
        name: "Diners Club", //大来卡
        pattern: /^3(0[0-5]|[68]\d)\d{11,16}$/, //校验规则第一位3 第二位 0或者68 第三位 如果第二位是0 那么第三位只能是0-5 如果是6或者8 匹配随便一个数字
        eagerPattern: /^3(0|[68])/, // 校验规则第一位3 第二位 0或者68
        imgUrl: "/statics/cart/accepted_payment_brand/diners_club.svg"
    },
    {
        name: "Visa", //美国的卡组织
        pattern: /^4\d{12}(\d{3}|\d{6})?$/,
        eagerPattern: /^4/,
        imgUrl: "/statics/cart/accepted_payment_brand/visa.svg"
    },
    {
        name: "Discover", // 美国
        pattern: /^6(011(0[0-9]|[2-4]\d|74|7[7-9]|8[6-9]|9[0-9])|4[4-9]\d{3}|5\d{4})\d{10}$/,
        eagerPattern: /^6(011(0[0-9]|[2-4]|74|7[7-9]|8[6-9]|9[0-9])|4[4-9]|5)/,
        imgUrl: "/statics/cart/accepted_payment_brand/discover.svg"
    },
    {
        name: "Elo", //巴西主流借记和信用卡
        pattern: /^(4[035]|5[0]|6[235])(6[7263]|9[90]|1[2416]|7[736]|8[9]|0[04579]|5[0])([0-9])([0-9])\d{10}$/,
        eagerPattern: /^(4[035]|5[0]|6[235])(6[7263]|9[90]|1[2416]|7[736]|8[9]|0[04579]|5[0])([0-9])([0-9])/,
        imgUrl: "/statics/cart/accepted_payment_brand/elo.svg"
    },
    {
        name: "JCB", // 日本的卡组织
        pattern: /^35\d{14}$/,
        eagerPattern: /^35/,
        imgUrl: "/statics/cart/accepted_payment_brand/jcb.svg"
    },
    {
        name: "Dankort", //丹麦卡支付
        pattern: /^5019\d{12}$/, //校验规则开头为5019 拼接12位数字
        eagerPattern: /^5019/, // 校验规则开头为5019
        imgUrl: "/statics/cart/accepted_payment_brand/dankost.svg"
    },

    {
        name: "Forbrugsforeningen", //Forbrugsforeningen是丹麦最古老的采购协会
        pattern: /^600722\d{10}$/,
        eagerPattern: /^600/,
        imgUrl: "/statics/cart/accepted_payment_brand/forbrugsforeningen.svg"
    },
    {
        name: "Maestro", //万事达借记卡
        pattern: /^(?:5[06789]\d\d|(?!6011[0234])(?!60117[4789])(?!60118[6789])(?!60119)(?!64[456789])(?!65)6\d{3})\d{8,15}$/,
        eagerPattern: /^(5(018|0[23]|[68])|6[37]|60111|60115|60117([56]|7[56])|60118[0-5]|64[0-3]|66)/,
        imgUrl: "/statics/cart/accepted_payment_brand/maestro.svg"
    },
    {
        name: "Mastercard", //万事达信用卡
        pattern: /^(5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)\d{12}$/,
        eagerPattern: /^(2[3-7]|22[2-9]|5[1-5])/,
        imgUrl: "/statics/cart/accepted_payment_brand/mastercard.svg"
    },

    {
        name: "Mir", //俄罗斯支付
        pattern: /^220[0-4]\d{12}$/,
        eagerPattern: /^220[0-4]/,
        imgUrl: "/statics/cart/accepted_payment_brand/mir.svg"
    },
    {
        name: "Troy", //土耳其本地信用卡
        pattern: /^9792\d{12}$/,
        eagerPattern: /^9792/,
        imgUrl: "/statics/cart/accepted_payment_brand/troy.svg"
    },
    {
        name: "Mada", //中东主流支付
        pattern:
            /^(4(0(0861|1757|3024|6136|6996|7(197|395)|9201)|1(2565|0621|0685|7633|9593)|2(0132|1141|281(7|8|9)|689700|8(331|67(1|2|3)))|3(1361|2328|4107|9954)|4(0(533|647|795)|5564|6(393|404|672))|5(5(036|708)|7865|7997|8456)|6(2220|854(0|1|2|3))|7(4491)|8(301(0|1|2)|4783|609(4|5|6)|931(7|8|9))|93428)|5(0(4300|6968|8160)|13213|2(0058|1076|4(130|514)|9(415|741))|3(0(060|906)|1(095|196)|2013|5(825|989)|6023|7767|9931)|4(3(085|357)|9760)|5(4180|7606|8563|8848)|8(5265|8(8(4(5|6|7|8|9)|5(0|1))|98(2|3))|9(005|206)))|6(0(4906|5141)|36120)|9682(0(1|2|3|4|5|6|7|8|9)|1(0|1)))\d{10}$/,
        eagerPattern:
            /^(4(0(0861|1757|3024|6136|6996|7(197|395)|9201)|1(2565|0621|0685|7633|9593)|2(0132|1141|281(7|8|9)|689700|8(331|67(1|2|3)))|3(1361|2328|4107|9954)|4(0(533|647|795)|5564|6(393|404|672))|5(5(036|708)|7865|7997|8456)|6(2220|854(0|1|2|3))|7(4491)|8(301(0|1|2)|4783|609(4|5|6)|931(7|8|9))|93428)|5(0(4300|6968|8160)|13213|2(0058|1076|4(130|514)|9(415|741))|3(0(060|906)|1(095|196)|2013|5(825|989)|6023|7767|9931)|4(3(085|357)|9760)|5(4180|7606|8563|8848)|8(5265|8(8(4(5|6|7|8|9)|5(0|1))|98(2|3))|9(005|206)))|6(0(4906|5141)|36120)|9682(0(1|2|3|4|5|6|7|8|9)|1(0|1)))/,
        imgUrl: "/statics/cart/accepted_payment_brand/mada.svg"
    },
    {
        name: "Meeza", //中东主流支付
        pattern: /^5078(03|09|10|11)\d{10}$/,
        eagerPattern: /^5078(03|09|10|11)/,
        imgUrl: "/statics/cart/accepted_payment_brand/meeza.svg"
    },
    {
        name: "UATP", //国航
        pattern: /^1\d{14}$/,
        eagerPattern: /^1/,
        imgUrl: "/statics/cart/accepted_payment_brand/uatp.svg"
    },
    {
        name: "UnionPay", //银联支付
        pattern: /^62[0-5]\d{13,16}$/,
        eagerPattern: /^62/,
        imgUrl: "/statics/cart/accepted_payment_brand/unionpay.svg"
    }
];

/**
 * 对输入的数字进行卡号类型的测试
 * @param {number} number - 要测试的数字
 * @param {boolean} eager - 是否使用快速模式进行测试
 * @returns {object} - 返回匹配的卡号类型
 */
function cardsTest(number, eager) {
    return cardTypes.find((type) => {
        return type[eager ? "eagerPattern" : "pattern"].test(number);
    });
}

/**
 * 获取卡类型名称
 * @param {string} number - 卡号
 * @returns {string} - 卡类型名称
 */
function getCardTypeName(number) {
    const card = cardsTest(number, true);
    return !card ? "" : card.name;
}

/**
 * 验证卡号
 *
 * @param {number} number - 卡号
 * @returns {boolean} - 验证结果，true为通过，false为不通过
 */
function verifyCard(number) {
    const card = cardsTest(number);
    return !card ? false : true;
}
